# -*- coding: utf-8 -*-
# @Time: 2021/11/27 21:54
# @Author: lijinxi
# @File    : time_slot_delay.py
# @desc

import numpy
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator


def load_data(file):
    delay = [0, ]
    with open(file, 'r') as f:
        for line in f:
            x_1, x_2, x_3 = list(map(float, line.strip("\n").split(" ")))
            delay.append(x_3 + x_2 + x_1)
    return delay


oss_delay = load_data("oss")
qo_delay = load_data("qo")
so_delay = load_data("so")


def plot_line(l1, l2, l3):
    x = [i for i in range(0, len(l1))]
    _l1 = numpy.array(l1) / 1000 * 5
    _l2 = numpy.array(l2) / 1000 * 5
    _l3 = numpy.array(l3) / 1000 * 5
    ax = plt.subplot(111)
    plt.figure(figsize=(12, 8))

    plt.plot(x, _l1, color='orange', ls='--', linewidth=2, marker='o', markersize=10, label='QO',
             markerfacecolor='none')
    plt.plot(x, _l2, color='g', ls='--', linewidth=2, marker='^', markersize=10, label='SO',
             markerfacecolor='none')
    plt.plot(x, _l3, ls='--', color='slateblue', linewidth=2, marker='s', markersize=10, label='OSS',
             markerfacecolor='none')
    plt.legend(fontsize=26, frameon=False, ncol=3)
    plt.axhline(y=5, color='lightgray', ls=':', linewidth=1)
    plt.axhline(y=10, color='lightgray', ls=':', linewidth=1)
    plt.axhline(y=15, color='lightgray', ls=':', linewidth=1)
    plt.axhline(y=20, color='lightgray', ls=':', linewidth=1)
    plt.axhline(y=25, color='lightgray', ls=':', linewidth=1)
    # plt.title('Total delay in each time slot.')
    axes = plt.gca()
    plt.ylim([0, 30])
    plt.xlim([0, 20])
    xmajorLocator = MultipleLocator(2)  # 将x主刻度标签设置为20的倍数
    axes.xaxis.set_major_locator(xmajorLocator)
    plt.tick_params(labelsize=22)  # 刻度字体大小13
    plt.xlabel('Time Slot', fontsize=26)
    plt.ylabel('Delay (s)', fontsize=26)
    # plt.show()
    plt.savefig('timeslot.pdf', bbox_inches='tight')
    plt.show()


plot_line(qo_delay, so_delay, oss_delay)
